package com.sam.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author: wm.xue
 * @date: 2021/11/3 14:03
 * @description:
 */
public class _00_MyConsumer {

    public static void main(String[] args) {

        Map<String, Object> configs = new HashMap<>();
        configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "10.100.17.117:9092");
        // 注意
        configs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        configs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

        // group.id 很重要
        configs.put(ConsumerConfig.GROUP_ID_CONFIG, "_00_myConsumer");

        configs.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(configs);
        consumer.subscribe(Arrays.asList("tp_tx_02"));

        while (true) {
            ConsumerRecords<String, String> consumerRecords = consumer.poll(Duration.ofSeconds(2));
//            consumerRecords.forEach(new Consumer<ConsumerRecord<String, String>>() {
//                @Override
//                public void accept(ConsumerRecord<String, String> record) {
//                    record.topic();
//                    record.offset();
//                }
//            });
            consumerRecords.forEach(record -> {
                System.err.println("消息的主题：" + record.topic());
                System.err.println("消息的分区：" + record.partition());
                System.err.println("消息的偏移量：" + record.offset());
                System.err.println("消息的内容：" +record.value());
            });
        }

//        consumer.close();


    }
}
